home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / SCIENTIF / H381.ZIP / GSRC208A.ZIP / DATAB.C < prev    next >
C/C++ Source or Header  |  1993-08-24  |  26KB  |  845 lines

  1. #include "copyleft.h"
  2.  
  3. /*
  4.     GEPASI - a simulator of metabolic pathways and other dynamical systems
  5.     Copyright (C) 1989, 1992  Pedro Mendes
  6. */
  7.  
  8. /*************************************/
  9. /*                                   */
  10. /*   databases and their functions   */
  11. /*                                   */
  12. /*        Zortech C/C++ 3.0 r4       */
  13. /*          MICROSOFT C 6.00         */
  14. /*          Visual C/C++ 1.0         */
  15. /*           QuickC/WIN 1.0          */
  16. /*             ULTRIX cc             */
  17. /*              GNU gcc              */
  18. /*                                   */
  19. /*   (include here compilers that    */
  20. /*   compiled GEPASI successfully)   */
  21. /*                                   */
  22. /*************************************/
  23.  
  24.  
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28.  
  29. #ifdef _ZTC
  30. #define MEM_DEBUG 1
  31. #include "mem.h"
  32. #else
  33. #define mem_malloc malloc
  34. #define mem_free free
  35. #define mem_realloc realloc
  36. #endif
  37.  
  38. #include "globals.h"
  39. #include "globvar.h"
  40. #include "lsoda.h"
  41. #include "strtbl.h"
  42. #include "heapchk.h"
  43.  
  44. /* structure holding information relative to kinetic mechanisms */
  45.  
  46. struct kint {
  47.                 unsigned char nsub;             /* number ofsubstrates  */
  48.                 unsigned char npro;             /* number of products   */
  49.                 unsigned char nmodf;    /* number of modifiers  */
  50.                 unsigned char revers;   /* reversability                */
  51.                 unsigned char nconst;   /* number of kin. const.*/
  52.                 char *descr;                    /* title                                */
  53.                 char *constnam;                 /* kin. constant names  */
  54.             };
  55.  
  56. /* structures for user-defined rate equations                                   */
  57.  
  58. struct nodet{
  59.          char item;
  60.          unsigned char val;
  61.          unsigned char left;
  62.          unsigned char right;
  63.         } ;
  64.  
  65. struct treet{
  66.          struct nodet node[256];
  67.          char id[64][10];
  68.          float constant[32];
  69.          int nnode,
  70.          nnum,
  71.          nid,
  72.          nsub,
  73.          npro,
  74.          nmodf,
  75.          nconst,
  76.          revers;
  77.          char descr[64];
  78.         } ;
  79.  
  80. /* structure holding simulation options                                                 */
  81.  
  82. struct opt {
  83.         int        dyn;
  84.         long    pfo;
  85.         double    endtime;
  86.         double    reltol;
  87.         double    abstol;
  88.         double    hrcz;
  89.         int        adams;
  90.         int        bdf;
  91.         int        ss;
  92.         int        debug;
  93.         int        txt;
  94.         int        structan;
  95.         int        staban;
  96.         int        stdela;
  97.         int        nonela;
  98.         int        stdcc;
  99.         int        noncc;
  100.         int        dat;
  101.         char    datname[PWIDTH];
  102.         int        datsep;
  103.         int        datwidth;
  104.         int        dattit;
  105.         int        datmca;
  106.         int        datss;
  107.         int        append;
  108.         int        quotes;
  109.         char    timeu[32];
  110.         char    concu[32];
  111.         int        scan;
  112.         int        scandens;
  113.         int        scanlog;
  114.        };
  115.  
  116. struct sp{
  117.           PDBL var;
  118.           double low;
  119.           double high;
  120.           double ampl;
  121.           int dens;
  122.           int log;
  123.           PDBL linkedto;
  124.           double factor;
  125.           int operation;
  126.          };
  127.  
  128. PDBL    params[MAX_STEP];                                                       /* ptr to parameters for each rate eq.  */
  129. int     *eff[MAX_STEP];                                                         /* ptr to parameters for each rate eq.  */
  130. unsigned char    revers[MAX_STEP];                                       /* 1 if reaction is reversible                  */
  131. struct  opt options;                                                            /* structure with simulation options    */
  132. struct  kint *ktype;                                                            /* ptr array of kinetic types & proprt  */
  133. struct  sp *sparam;                                                                     /* ptr array with parameters to scan    */
  134. PDBL    *scanpar;                                                                       /* ptrs to all possbl.params 4 scanning */
  135. PDBL    *outpel;                                                                        /* ptrs to all possbl. values for output*/
  136. PDBL    *poutelem;                                                                        /* ptrs to values for actual dat output */
  137. char    *outtit;                                                                        /* pointer to buffer with column titles */
  138. char    *treestr;                                                                       /* pointer to buffer with constant names*/
  139. char    *treeptr;                                                                       /* pointer to buffer with constant names*/
  140. int     *scindex;                                                                       /* ptr to base of array with idx to scan*/
  141. int     *lindex;                                                                        /* ptr to base of array with idx 2 links*/
  142. int     nscanpar;                                   /* number of scanning elements                      */
  143. int     noutpel;                                    /* number of output elements                        */
  144. int     totscan;                                    /* number of selected scanning params       */
  145. int     nlinks;                                                                         /* number of linked parameters                  */
  146. int     nudf;                                                                           /* number of user-def functions                 */
  147. int     nrateq;                                                                         /* number of total rate equations               */
  148. int     totsel;                                     /* number of selected output elements       */
  149. int     kfl[MAX_STEP];                                                          /* flags for kinetic types                              */
  150. unsigned int sizespar;
  151. unsigned int sizeparam;
  152. unsigned int sizeeff;
  153. unsigned int sizeoutp;
  154. unsigned int sizetr;
  155. struct  treet *tree;                                                            /* function tree for rate equations             */
  156. struct  treet tr;                                                                       /* tree for the input                                   */
  157.  
  158. /* main point for memory block allocation               */
  159. int MemAlloc( void )
  160. {
  161.  int i;
  162.  
  163.  
  164.  sizeparam = sizeof( double );
  165.  params[0] = (double *) mem_malloc( sizeparam );
  166.  if( params[0] == NULL ) return -1;
  167.  /* make the other params[i] point to NULL                                                                */
  168.  for( i=1; i<MAX_STEP; i++ ) params[i] = NULL;
  169.  
  170.  sizeeff = 1;
  171.  eff[0] = (int *) mem_malloc( sizeeff * sizeof( int ) );
  172.  if( eff[0] == NULL )
  173.  {
  174.   mem_free( params[0] );
  175.   return -1;
  176.  }
  177.  /* make the other eff[i] point to NULL                                                                   */
  178.  for( i=1; i<MAX_STEP; i++ ) eff[i] = NULL;
  179.  
  180.  /* allocate space for the MAX_TYPE descriptors of kinetic types    */
  181.  ktype = (struct kint *) mem_malloc( MAX_TYP * sizeof( struct kint ) );
  182.  if( ktype == NULL )
  183.  {
  184.   mem_free( params[0] );
  185.   mem_free( eff[0] );
  186.   return -1;
  187.  }
  188.  
  189.  sizeoutp = 4 * sizeof( PDBL );
  190.  outpel = (PDBL *) mem_malloc( sizeoutp );
  191.  if( outpel == NULL )
  192.  {
  193.   mem_free( ktype );
  194.   mem_free( params[0] );
  195.   mem_free( eff[0] );
  196.   return -1;
  197.  }
  198.  
  199.  poutelem = (PDBL *) mem_malloc( sizeof(PDBL) );
  200.  if( poutelem == NULL )
  201.  {
  202.   mem_free( outpel );
  203.   mem_free( ktype );
  204.   mem_free( params[0] );
  205.   mem_free( eff[0] );
  206.   return -1;
  207.  }
  208.  
  209.  loop = ( unsigned char (*)[MAX_STEP][MAX_MET] ) mem_malloc( MAX_STEP * MAX_MET * sizeof( unsigned char ) );
  210.  if( loop == NULL )
  211.  {
  212.   mem_free( poutelem );
  213.   mem_free( outpel );
  214.   mem_free( ktype );
  215.   mem_free( params[0] );
  216.   mem_free( eff[0] );
  217.   return -1;
  218.  }
  219.  
  220.  rstr = ( int (*)[MAX_STEP][MAX_MOL] ) mem_malloc( MAX_STEP * MAX_MOL * sizeof( int ) );
  221.  if( rstr == NULL )
  222.  {
  223.   mem_free( loop );
  224.   mem_free( poutelem );
  225.   mem_free( outpel );
  226.   mem_free( ktype );
  227.   mem_free( params[0] );
  228.   mem_free( eff[0] );
  229.   return -1;
  230.  }
  231.  
  232.  outtit = ( char * ) mem_malloc( 25 * sizeof( char ) );
  233.  if( outtit == NULL )
  234.  {
  235.   mem_free( rstr );
  236.   mem_free( loop );
  237.   mem_free( poutelem );
  238.   mem_free( outpel );
  239.   mem_free( ktype );
  240.   mem_free( params[0] );
  241.   mem_free( eff[0] );
  242.   return -1;
  243.  }
  244.  
  245.  sparam = (struct sp *) mem_malloc( sizeof( struct sp ) );
  246.  if( sparam == NULL )
  247.  {
  248.   mem_free( outtit );
  249.   mem_free( rstr );
  250.   mem_free( loop );
  251.   mem_free( poutelem );
  252.   mem_free( outpel );
  253.   mem_free( ktype );
  254.   mem_free( params[0] );
  255.   mem_free( eff[0] );
  256.   return -1;
  257.  }
  258.  
  259.  sizespar = 4 * sizeof( PDBL );
  260.  scanpar = (PDBL *) mem_malloc( sizespar );
  261.  if( scanpar == NULL )
  262.  {
  263.   mem_free( sparam );
  264.   mem_free( outtit );
  265.   mem_free( rstr );
  266.   mem_free( loop );
  267.   mem_free( poutelem );
  268.   mem_free( outpel );
  269.   mem_free( ktype );
  270.   mem_free( params[0] );
  271.   mem_free( eff[0] );
  272.   return -1;
  273.  }
  274.  
  275.  scindex = (int *) mem_malloc( 4 * sizeof(int) );
  276.  if( scindex == NULL )
  277.  {
  278.   mem_free( scanpar );
  279.   mem_free( sparam );
  280.   mem_free( outtit );
  281.   mem_free( rstr );
  282.   mem_free( loop );
  283.   mem_free( poutelem );
  284.   mem_free( outpel );
  285.   mem_free( ktype );
  286.   mem_free( params[0] );
  287.   mem_free( eff[0] );
  288.   return -1;
  289.  }
  290.  
  291.  lindex = (int *) mem_malloc( sizeof(int) );
  292.  if( lindex == NULL )
  293.  {
  294.   mem_free( scindex );
  295.   mem_free( scanpar );
  296.   mem_free( sparam );
  297.   mem_free( outtit );
  298.   mem_free( rstr );
  299.   mem_free( loop );
  300.   mem_free( poutelem );
  301.   mem_free( outpel );
  302.   mem_free( ktype );
  303.   mem_free( params[0] );
  304.   mem_free( eff[0] );
  305.   return -1;
  306.  }
  307.  
  308.  tree = (struct treet *) mem_malloc( sizeof( struct treet ) );
  309.  if( tree == NULL )
  310.  {
  311.   mem_free( lindex );
  312.   mem_free( scindex );
  313.   mem_free( scanpar );
  314.   mem_free( sparam );
  315.   mem_free( outtit );
  316.   mem_free( rstr );
  317.   mem_free( loop );
  318.   mem_free( poutelem );
  319.   mem_free( outpel );
  320.   mem_free( ktype );
  321.   mem_free( params[0] );
  322.   mem_free( eff[0] );
  323.   return -1;
  324.  }
  325.  
  326.  sizetr = 4;
  327.  treestr = (char *) mem_malloc( sizetr * sizeof( char ) );
  328.  if( treestr == NULL )
  329.  {
  330.   mem_free( tree );
  331.   mem_free( lindex );
  332.   mem_free( scindex );
  333.   mem_free( scanpar );
  334.   mem_free( sparam );
  335.   mem_free( outtit );
  336.   mem_free( rstr );
  337.   mem_free( loop );
  338.   mem_free( poutelem );
  339.   mem_free( outpel );
  340.   mem_free( ktype );
  341.   mem_free( params[0] );
  342.   mem_free( eff[0] );
  343.   return -1;
  344.  }
  345.  
  346.  return 0;
  347. }
  348.  
  349. /* Initialization of databases */
  350.  
  351. void InitDataBase( void )
  352. {
  353.  ktype[NOT].nsub = 0;                                                           /* init info of kinetic types   */
  354.  ktype[NOT].npro = 0;
  355.  ktype[NOT].nconst = 0;
  356.  ktype[NOT].nmodf = 0;
  357.  ktype[NOT].constnam = "";
  358.  ktype[NOT].descr = "<not defined>";
  359.  ktype[I01].nsub = 0;
  360.  ktype[I01].npro = 1;
  361.  ktype[I01].nconst = 1;
  362.  ktype[I01].nmodf = 0;
  363.  ktype[I01].constnam = "k";
  364.  ktype[I01].descr = "constant rate";
  365.  ktype[I10].nsub = 1;
  366.  ktype[I10].npro = 0;
  367.  ktype[I10].nconst = 1;
  368.  ktype[I10].nmodf = 0;
  369.  ktype[I10].constnam = ktype[I01].constnam;
  370.  ktype[I10].descr = ktype[I01].descr;
  371.  ktype[I11].nsub = 1;
  372.  ktype[I11].npro = 1;
  373.  ktype[I11].revers = 0;
  374.  ktype[I11].nconst = 1;
  375.  ktype[I11].nmodf = 0;
  376.  ktype[I11].constnam = ktype[I01].constnam;
  377.  ktype[I11].descr = "mass action";
  378.  ktype[R11].nsub = 1;
  379.  ktype[R11].npro = 1;
  380.  ktype[R11].nconst = 2;
  381.  ktype[R11].nmodf = 0;
  382.  ktype[R11].constnam = "k1\0k2";
  383.  ktype[R11].descr = ktype[I11].descr;
  384.  ktype[I21].nsub = 2;
  385.  ktype[I21].npro = 1;
  386.  ktype[I21].nconst = 1;
  387.  ktype[I21].nmodf = 0;
  388.  ktype[I21].constnam = ktype[I01].constnam;
  389.  ktype[I21].descr = ktype[I11].descr;
  390.  ktype[R21].nsub = 2;
  391.  ktype[R21].npro = 1;
  392.  ktype[R21].nconst = 2;
  393.  ktype[R21].nmodf = 0;
  394.  ktype[R21].constnam = ktype[R11].constnam;
  395.  ktype[R21].descr = ktype[I11].descr;
  396.  ktype[I12].nsub = 1;
  397.  ktype[I12].npro = 2;
  398.  ktype[I12].nconst = 1;
  399.  ktype[I12].nmodf = 0;
  400.  ktype[I12].constnam = ktype[I01].constnam;
  401.  ktype[I12].descr = ktype[I11].descr;
  402.  ktype[R12].nsub = 1;
  403.  ktype[R12].npro = 2;
  404.  ktype[R12].nconst = 2;
  405.  ktype[R12].nmodf = 0;
  406.  ktype[R12].constnam = ktype[R11].constnam;
  407.  ktype[R12].descr = ktype[I11].descr;
  408.  ktype[I31].nsub = 3;
  409.  ktype[I31].npro = 1;
  410.  ktype[I31].nconst = 1;
  411.  ktype[I31].nmodf = 0;
  412.  ktype[I31].constnam = ktype[I01].constnam;
  413.  ktype[I31].descr = ktype[I11].descr;
  414.  ktype[R31].nsub = 3;
  415.  ktype[R31].npro = 1;
  416.  ktype[R31].nconst = 2;
  417.  ktype[R31].nmodf = 0;
  418.  ktype[R31].constnam = ktype[R11].constnam;
  419.  ktype[R31].descr = ktype[I11].descr;
  420.  ktype[I13].nsub = 1;
  421.  ktype[I13].npro = 3;
  422.  ktype[I13].nconst = 1;
  423.  ktype[I13].nmodf = 0;
  424.  ktype[I13].constnam = ktype[I01].constnam;
  425.  ktype[I13].descr = ktype[I11].descr;
  426.  ktype[R13].nsub = 1;
  427.  ktype[R13].npro = 3;
  428.  ktype[R13].nconst = 2;
  429.  ktype[R13].nmodf = 0;
  430.  ktype[R13].constnam = ktype[R11].constnam;
  431.  ktype[R13].descr = ktype[I11].descr;
  432.  ktype[I22].nsub = 2;
  433.  ktype[I22].npro = 2;
  434.  ktype[I22].nconst = 1;
  435.  ktype[I22].nmodf = 0;
  436.  ktype[I22].constnam = ktype[I01].constnam;
  437.  ktype[I22].descr = ktype[I11].descr;
  438.  ktype[R22].nsub = 2;
  439.  ktype[R22].npro = 2;
  440.  ktype[R22].nconst = 2;
  441.  ktype[R22].nmodf = 0;
  442.  ktype[R22].constnam = ktype[R11].constnam;
  443.  ktype[R22].descr = ktype[I11].descr;
  444.  ktype[I32].nsub = 3;
  445.  ktype[I32].npro = 2;
  446.  ktype[I32].nconst = 1;
  447.  ktype[I32].nmodf = 0;
  448.  ktype[I32].constnam = ktype[I01].constnam;
  449.  ktype[I32].descr = ktype[I11].descr;
  450.  ktype[R32].nsub = 3;
  451.  ktype[R32].npro = 2;
  452.  ktype[R32].nconst = 2;
  453.  ktype[R32].nmodf = 0;
  454.  ktype[R32].constnam = ktype[R11].constnam;
  455.  ktype[R32].descr = ktype[I11].descr;
  456.  ktype[I23].nsub = 2;
  457.  ktype[I23].npro = 3;
  458.  ktype[I23].nconst = 1;
  459.  ktype[I23].nmodf = 0;
  460.  ktype[I23].constnam = ktype[I01].constnam;
  461.  ktype[I23].descr = ktype[I11].descr;
  462.  ktype[R23].nsub = 2;
  463.  ktype[R23].npro = 3;
  464.  ktype[R23].nconst = 2;
  465.  ktype[R23].nmodf = 0;
  466.  ktype[R23].constnam = ktype[R11].constnam;
  467.  ktype[R23].descr = ktype[I11].descr;
  468.  ktype[I33].nsub = 3;
  469.  ktype[I33].npro = 3;
  470.  ktype[I33].nconst = 1;
  471.  ktype[I33].nmodf = 0;
  472.  ktype[I33].constnam = ktype[I01].constnam;
  473.  ktype[I33].descr = ktype[I11].descr;
  474.  ktype[R33].nsub = 3;
  475.  ktype[R33].npro = 3;
  476.  ktype[R33].nconst = 2;
  477.  ktype[R33].nmodf = 0;
  478.  ktype[R33].constnam = ktype[R11].constnam;
  479.  ktype[R33].descr = ktype[I11].descr;
  480.  ktype[IMM].nsub = 1;
  481.  ktype[IMM].npro = 1;
  482.  ktype[IMM].nconst = 2;
  483.  ktype[IMM].nmodf = 0;
  484.  ktype[IMM].constnam = "Km\0V";
  485.  ktype[IMM].descr = "Michaelis-Menten";
  486.  ktype[RMM].nsub = 1;
  487.  ktype[RMM].npro = 1;
  488.  ktype[RMM].nconst = 4;
  489.  ktype[RMM].nmodf = 0;
  490.  ktype[RMM].constnam = "Kms\0Kmp\0Vf\0Vr";
  491.  ktype[RMM].descr = "Reversible Michaelis-Menten";
  492.  ktype[PSI].nsub = 1;
  493.  ktype[PSI].npro = 1;
  494.  ktype[PSI].nconst = 5;
  495.  ktype[PSI].nmodf = 1;
  496.  ktype[PSI].constnam = "Kms\0Kmp\0Vf\0Vr\0Ki";
  497.  ktype[PSI].descr = "Specific inhibition";
  498.  ktype[PCI].nsub = 1;
  499.  ktype[PCI].npro = 1;
  500.  ktype[PCI].nconst = 5;
  501.  ktype[PCI].nmodf = 1;
  502.  ktype[PCI].constnam = ktype[PSI].constnam;
  503.  ktype[PCI].descr = "Catalytic inhibition";
  504.  ktype[MXI].nsub = 1;
  505.  ktype[MXI].npro = 1;
  506.  ktype[MXI].nconst = 6;
  507.  ktype[MXI].nmodf = 1;
  508.  ktype[MXI].constnam = "Kms\0Kmp\0Vf\0Vr\0Ki\0Ki'";
  509.  ktype[MXI].descr = "Mixed inhibition";
  510.  ktype[PSA].nsub = 1;
  511.  ktype[PSA].npro = 1;
  512.  ktype[PSA].nconst = 5;
  513.  ktype[PSA].nmodf = 1;
  514.  ktype[PSA].constnam = "Kms\0Kmp\0Vf\0Vr\0Ka";
  515.  ktype[PSA].descr = "Specific activation";
  516.  ktype[PCA].nsub = 1;
  517.  ktype[PCA].npro = 1;
  518.  ktype[PCA].nconst = 5;
  519.  ktype[PCA].nmodf = 1;
  520.  ktype[PCA].constnam = ktype[PSA].constnam;
  521.  ktype[PCA].descr = "Catalytic activation";
  522.  ktype[MXA].nsub = 1;
  523.  ktype[MXA].npro = 1;
  524.  ktype[MXA].nconst = 6;
  525.  ktype[MXA].nmodf = 1;
  526.  ktype[MXA].constnam = "Kms\0Kmp\0Vf\0Vr\0Ka\0Ka'";
  527.  ktype[MXA].descr = "Mixed activation";
  528.  ktype[GOM].nsub = 1;
  529.  ktype[GOM].npro = 1;
  530.  ktype[GOM].nconst = 6;
  531.  ktype[GOM].nmodf = 1;
  532.  ktype[GOM].constnam = "Kms\0Kmp\0Vf\0Vr\0Ki\0Ki'";
  533.  ktype[GOM].descr = "Botts-Morales";
  534.  ktype[HIL].nsub = 1;
  535.  ktype[HIL].npro = 1;
  536.  ktype[HIL].nconst = 3;
  537.  ktype[HIL].nmodf = 0;
  538.  ktype[HIL].constnam = "Km\0V\0nh";
  539.  ktype[HIL].descr = "Hill";
  540.  ktype[UBS].nsub = 1;
  541.  ktype[UBS].npro = 2;
  542.  ktype[UBS].nconst = 6;
  543.  ktype[UBS].nmodf = 0;
  544.  ktype[UBS].constnam = "Ka\0Kp\0Kq\0Kpq\0Vf\0Vr";
  545.  ktype[UBS].descr = "Ordered Uni Bi (A=P+Q)";
  546.  ktype[UBM].nsub = 1;
  547.  ktype[UBM].npro = 1;
  548.  ktype[UBM].nconst = 6;
  549.  ktype[UBM].nmodf = 0;
  550.  ktype[UBM].constnam = "Ka\0Kp\0Vf\0Vr";
  551.  ktype[UBM].descr = "Ordered Uni Bi (A=2*P)";
  552.  ktype[ALI].nsub = 1;
  553.  ktype[ALI].npro = 1;
  554.  ktype[ALI].nconst = 6;
  555.  ktype[ALI].nmodf = 1;
  556.  ktype[ALI].constnam = "Kms\0Kmp\0Vf\0Vr\0Ki\0n";
  557.  ktype[ALI].descr = "Allosteric inhibition";
  558. }
  559.  
  560. /*
  561.     set up the pointers to params
  562. */
  563.  
  564. int SetParams( void )
  565. {
  566.  int i, j;
  567.  
  568.  /* calculate the number of kinetic parameters                  */
  569.  for( i=0, j=0; i<nsteps; i++ )
  570.   j += ktype[kinetype[i]].nconst;
  571.  /* and the space they take in memory                                   */
  572.  sizeparam = j * sizeof( double );
  573.  /* reallocate memory for parameters                                    */
  574.  /* setting already the first (0) pointer                               */
  575.  params[0] = (double *) mem_realloc( params[0], sizeparam );
  576.  if( params[0] == NULL )
  577.  {
  578.   return -1;
  579.  }
  580.  /* set the other pointers                                                              */
  581.  for( i=1; i<nsteps; i++ )
  582.   params[i] = params[i-1] + ktype[kinetype[i-1]].nconst;
  583.  /* return successfuly                                                                  */
  584.  return 0;
  585. }
  586.  
  587. /*
  588.     set up the pointers to eff (indexes of effectors)
  589. */
  590.  
  591. int SetEff( void )
  592. {
  593.  int i, j;
  594.  
  595.  /* calculate the total number of effectors                     */
  596.  for( i=0, sizeeff=0; i<nsteps; i++ )
  597.  {             
  598.   j = ktype[kinetype[i]].nsub +
  599.       ktype[kinetype[i]].npro +
  600.       ktype[kinetype[i]].nmodf;
  601.   sizeeff += j;
  602.  }
  603.  
  604.  /* mem_reallocate memory for effector indexes                          */
  605.  /* setting already the first (0) pointer                               */
  606.  eff[0] = (int *) mem_realloc( eff[0], sizeeff * sizeof( int ) );
  607.  if( eff[0] == NULL )
  608.  {
  609.   return -1;
  610.  }
  611.  /* set the other pointers                                                              */
  612.  for( i=1; i<nsteps; i++ )
  613.   eff[i] = eff[i-1] + ( ktype[kinetype[i-1]].nsub +
  614.                         ktype[kinetype[i-1]].npro +
  615.                         ktype[kinetype[i-1]].nmodf );
  616.  /* return successfuly                                                                  */
  617.  return 0;
  618. }
  619.  
  620.  
  621. /* setup the output and scanning elements                               */
  622.  
  623. int SetOutpEl( void )
  624. {
  625.  int i, j, k, l;
  626.  
  627.  /* calculate the number of output elements                             */
  628.  /* all metabolite concentrations and step fluxes               */
  629.  noutpel = 2*totmet + nsteps;
  630.  nscanpar = totmet;
  631.  /* all kinetic constants...                                                     */
  632.  for( i=0; i<nsteps; i++ )
  633.  {
  634.   noutpel += ktype[kinetu[i]].nconst;
  635.   nscanpar += ktype[kinetu[i]].nconst;
  636.  }
  637.  /* all elasticities                                                                    */
  638.  noutpel += nsteps * totmet;
  639.  /* all concentration control coefficients                              */
  640.  noutpel += totmet * nsteps;
  641.  /* all flux control coefficients                                               */
  642.  noutpel += nsteps * nsteps;
  643.  /* endtime, rel & abs tol, nfunc, njac, nistep, smstep */
  644.  noutpel += 8;
  645.  nscanpar += 4;
  646.  
  647.  /* calculate the size of outpel and the string pool    */
  648.  sizeoutp = noutpel * sizeof( PDBL );
  649.  sizespar = nscanpar * sizeof( struct sp );
  650.  
  651.  /* reallocate memory for outpel                                                */
  652.  outpel = (PDBL *) mem_realloc( outpel, sizeoutp );
  653.  if( outpel == NULL )
  654.   return -1;
  655.  
  656.  /* mem_reallocate memory for sparam                                            */
  657.  sparam = (struct sp *) mem_realloc( sparam, sizespar );
  658.  if( sparam == NULL )
  659.   return -1;
  660.  
  661.  /* mem_reallocate memory for scindex                                           */
  662.  scindex = (int *) mem_realloc( scindex, nscanpar * sizeof( int ) );
  663.  if( scindex == NULL )
  664.  {
  665.   return -1;
  666.  }
  667.  
  668.  /* mem_reallocate memory for lindex                                            */
  669.  lindex = (int *) mem_realloc( lindex, nscanpar * sizeof( int ) );
  670.  if( lindex == NULL )
  671.  {
  672.   return -1;
  673.  }
  674.  
  675.  /* load the pointers                                                                   */
  676.  
  677.  /* initial concentrations                                                      */
  678.  for( i=l=0; i<totmet; i++, l++ )
  679.  {
  680.   outpel[i] = &(x0[i]);
  681.   sparam[l].var = &(x0[i]);
  682.  }
  683.  /* final concentrations                                                                */
  684.  for( j=0; j<totmet; i++, j++ )
  685.   outpel[i] = &(x[j]);
  686.  
  687.  /* fluxes                                                                              */
  688.  for( j=0; j<nsteps; j++, i++ )
  689.   outpel[i] = &(flux[j]);
  690.  
  691.  /* kinetic constants                                                                   */
  692.  for( j=0; j<nsteps; j++ )
  693.   for( k=0; k < (int) ktype[kinetype[j]].nconst; k++, i++, l++ )
  694.    outpel[i] = sparam[l].var = params[j]+k;
  695.  
  696.  /* elasticities                                                                        */
  697.   for( j=0; j<nsteps; j++ )
  698.    for(k=0;k<totmet;k++,i++)
  699.     outpel[i] = &Dxv[j][k];
  700.  
  701.  /* concentration control coefficients                                  */
  702.  for( j=0; j<totmet; j++ )
  703.    for(k=0;k<nsteps;k++,i++)
  704.     outpel[i] = &Gamma[j][k];
  705.  
  706.  /* flux control coefficients                                                   */
  707.  for( j=0; j<nsteps; j++ )
  708.   for(k=0;k<nsteps;k++,i++)
  709.    outpel[i] = &C[j][k];
  710.  
  711.  /* endtime, tolerance, nfunc, njacob, nistep, smstep   */
  712.  outpel[i++] = sparam[l++].var = &endtime;
  713.  outpel[i++] = &intst;
  714.  outpel[i++] = &nfeval;
  715.  outpel[i++] = &njeval;
  716.  outpel[i++] = &hu;
  717.  outpel[i++] = sparam[l++].var = &options.reltol;
  718.  outpel[i++] = sparam[l++].var = &options.abstol;
  719.  outpel[i]   = sparam[l].var = &options.hrcz;
  720.  
  721.  return 0;
  722. }
  723.  
  724. /*
  725.    create a new function tree object
  726. */
  727.  
  728. int new_tree( int idx )
  729. {
  730.  nudf++;
  731.  tree = (struct treet *) mem_realloc( (void *) tree, nudf * sizeof( struct treet ) );
  732.  if( tree == NULL )
  733.   return IDS_ERR_NOEXEC;
  734.  memcpy( (void *) &tree[idx], (void *) &tr, sizeof( struct treet ) );
  735.  return 0;
  736. }
  737.  
  738. /*
  739.   add a user-defined rate equation to the database
  740. */
  741.  
  742. int new_rateq( int idx )
  743. {
  744.  unsigned int strsize;
  745.  int i, pm, ef;
  746.  long l;
  747.  char *tmpptr;
  748.  
  749.  /* measure the required length for the constant names                          */
  750.  for( i=0, strsize=0; i<tree[idx].nid; i++ )
  751.   if( tree[idx].id[i][9] == (char) 0 )
  752.    strsize += strlen(tree[idx].id[i]) + 1;
  753.  sizetr += strsize * sizeof( char );
  754.  l = (long) (treeptr - treestr);
  755.  /* reallocate the buffer to hold new strings                                           */
  756.  treestr = (char *) mem_realloc( (void *) treestr, sizetr );
  757.  if( treestr == NULL )
  758.   return IDS_ERR_NOEXEC;
  759.  treeptr = treestr + l;
  760.  tmpptr = treeptr;
  761.  /* copy the strings to the buffer                                                                      */
  762.  for( i=0; i<tree[idx].nid; i++ )
  763.   if( tree[idx].id[i][9] == (char) 0 )
  764.   {
  765.    strcpy( treeptr, tree[idx].id[i] );
  766.    treeptr += strlen( tree[idx].id[i] ) + 1;
  767.   }
  768.  /* reuse the space in tree.id[idx][0] to store indexes                         */
  769.  for( i=0, ef=pm=0; i<tree[idx].nid; i++ )
  770.   switch( (int) tree[idx].id[i][9] )
  771.   {
  772.    case 0: tree[idx].id[i][0] = (char) pm; pm++; break; /* constant  */
  773.    case 1: tree[idx].id[i][0] = (char) ef; ef++;        /* substrate */
  774.   }
  775.  /* the first nsub are reserved for substrates even when not explicit*/
  776.  for( i=0, ef=tree[idx].nsub; i<tree[idx].nid; i++ )
  777.   if( (int) tree[idx].id[i][9] == 2 )                   /* product   */
  778.   {
  779.    tree[idx].id[i][0] = (char) ef; ef++;
  780.   }
  781.  /* nsub+npro are reserved for substs.& prods. even when not explicit*/
  782.  for( i=0, ef=tree[idx].nsub+tree[idx].npro; i<tree[idx].nid; i++ )
  783.   if( (int) tree[idx].id[i][9] == 3 )                   /* modifier  */
  784.   {
  785.    tree[idx].id[i][0] = (char) ef; ef++;
  786.   }
  787.  
  788.  /* expand the memory block                                                                             */
  789.  nrateq++;
  790.  ktype = (struct kint *) mem_realloc( ktype, nrateq * sizeof( struct kint ) );
  791.  if( ktype != NULL )
  792.  {
  793.   ktype[MAX_TYP+idx].nmodf    = (unsigned char) tree[idx].nmodf;
  794.   ktype[MAX_TYP+idx].nconst   = (unsigned char) tree[idx].nconst;
  795.   ktype[MAX_TYP+idx].nsub     = (unsigned char) tree[idx].nsub;
  796.   ktype[MAX_TYP+idx].npro     = (unsigned char) tree[idx].npro;
  797.   ktype[MAX_TYP+idx].descr    = tree[idx].descr;
  798.   ktype[MAX_TYP+idx].constnam = tmpptr;
  799.   return 0;
  800.  }
  801.  else
  802.   return IDS_ERR_NOEXEC;
  803. }
  804.  
  805. /*
  806.   add a new function read from a .TOP file
  807.   and renumber references to this type in the .TOP
  808. */
  809.  
  810. int addtr( int e )
  811. {
  812.  int i, j, k, nRc;
  813.  
  814.  j = nudf;
  815.  k = kinetu[e];
  816.  if( (nRc = new_tree( j )) != 0 ) return nRc;
  817.  if( (nRc = new_rateq( j )) != 0 ) return nRc;
  818.  for( i=e; i<nsteps; i++ )
  819.   if( (kinetu[i]==k) && (kfl[i]) )
  820.   {
  821.    kinetu[i] = MAX_TYP+j;
  822.    kfl[i] = 0;
  823.   }
  824.  return 0;
  825. }
  826.  
  827.  
  828. void FreeMem( void )
  829. {
  830.  mem_free( treestr );
  831.  mem_free( tree );
  832.  mem_free( lindex );
  833.  mem_free( scindex );
  834.  mem_free( scanpar );
  835.  mem_free( sparam );
  836.  mem_free( outtit );
  837.  mem_free( rstr );
  838.  mem_free( loop );
  839.  mem_free( poutelem );
  840.  mem_free( outpel );
  841.  mem_free( ktype );
  842.  mem_free( params[0] );
  843.  mem_free( eff[0] );
  844. }
  845.